#include <QMessageBox>
#include "dialogcfg.h"
#include "ui_dialogcfg.h"
#include "json_list.h"
#include "cJSON/cJSON.h"
#include "cJSON.h"
#include "mem_malloc.h"

#define  debug_log     0
#ifndef debug_log
#define  debug_log     1
#endif
//#undef   debug_log
#include <stdio.h>

//#ifdef debug_log
#if debug_log
#define pr_debug(fmt, ...) printf(fmt, ##__VA_ARGS__); fflush(stdout);
#else
#define pr_debug(fmt, ...) ;
#endif

DialogCFG::DialogCFG(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogCFG)
{
    ui->setupUi(this);
    ui->lineEdit_Host->setText("39.108.72.130");
    ui->lineEdit_Port->setText("9910");
    ui->lineEdit_GPS_Time->setText("10");

    // SSL
    ui->radioButton_SM2->setEnabled(false);
    ui->radioButton_INFO->setChecked(true);

    ui->radioButton_FuelType->setChecked(true);

    ui->radioButton_WIFI_Clode->setChecked(true);
}

DialogCFG::~DialogCFG()
{
    delete ui;
}
int DialogCFG::configure(const char *path)
{
    const char *Host = NULL;
    const char *CarType[] = {
        "FuelType",   // "汽油车"
        "DieselType", // "柴油车"
    };
    struct config_data cfg;
    memset(&cfg, 0, sizeof (cfg));
    Host = ui->lineEdit_Host->text().toUtf8().data();
    memcpy(cfg.Host, Host, strlen(Host));
    cfg.Port = ui->lineEdit_Port->text().toInt();
    if(ui->radioButton_INFO->isChecked()) cfg.ssl = CFG_SSL_INFO;
    if(ui->radioButton_RSA->isChecked()) cfg.ssl = CFG_SSL_RSA;
    if(ui->radioButton_SM2->isChecked()) cfg.ssl = CFG_SSL_SM2;
    if(ui->radioButton_FuelType->isChecked()) memcpy(cfg.CarType, CarType[0], strlen(CarType[0]));
    if(ui->radioButton_DieselType->isChecked()) memcpy(cfg.CarType, CarType[1], strlen(CarType[1]));
    cfg.GPS_Time = ui->lineEdit_GPS_Time->text().toInt();
    if(ui->radioButton_WIFI_Open->isChecked()) cfg.wifi = CFG_WiFi_Turn_On;
    if(ui->radioButton_WIFI_Clode->isChecked()) cfg.wifi = CFG_WiFi_Turn_Off;
    return create_configure(path, &cfg);
}
void DialogCFG::on_pushButton_CFG_clicked()
{
    // CFG
    //create_cfg("OBD.cfg");
    configure("./upload/OBD.cfg");
    QMessageBox *msgBox;
        msgBox = new QMessageBox("Config",		        ///--这里是设置消息框标题
            "配置文件已更新：OBD.cfg",						///--这里是设置消息框显示的内容
            QMessageBox::Information,						///--这里是在消息框显示的图标
            QMessageBox::Ok | QMessageBox::Default,		///---这里是显示消息框上的按钮情况
            QMessageBox::Cancel | QMessageBox::Escape,	///---这里与 键盘上的 escape 键结合。当用户按下该键，消息框将执行cancel按钮事件
            0);											///---这里是 定义第三个按钮， 该例子 只是 了显示2个按钮
        msgBox->show();
}

